//
//  UIBarButtonItem+Extension.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/23.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    //便利构造函数
    convenience init?(imageName:String,target:Any?,action:Selector) {
        
        //初始化一个按钮
        let btn = UIButton(type: .custom)
        //获得图片
        guard let imgNormal = UIImage(named: "\(imageName)_nor"),
              let imgPress = UIImage(named: "\(imageName)_press") else {
                return nil
        }
    
        btn.setBackgroundImage(imgNormal, for: .normal)
        btn.setBackgroundImage(imgPress, for: .highlighted)
        
        btn.frame = CGRect(x: 0, y: 0, width: imgNormal.size.width, height: imgPress.size.height)
        
        //添加监听事件
        btn.addTarget(target, action: action, for: .touchUpInside)
        self.init(customView:btn)
    }
}

